<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <!-- <link rel="stylesheet" href="../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all"> -->
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <form class="layui-form layuimini-form" lay-filter="edit-form">
        <input name=id value="" type="hidden" />
        <div class="layui-form-item">
            <label class="layui-form-label required">授权名称</label>
            <div class="layui-input-block">
                <input type="text" name="authName" autocomplete="off" lay-verify="required" lay-reqtext="授权名称不能为空"
                    placeholder="请输入授权名称" value="" class="layui-input">
                <tip>填写授权的名称。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">PID</label>
            <div class="layui-input-block">
                <input type="text" name="pid" disabled autocomplete="off"
                    placeholder="授权PID" value="" class="layui-input">
                <tip>授权的PID。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">PATH</label>
            <div class="layui-input-block">
                <input type="text" name="path" disabled autocomplete="off"
                    placeholder="授权PATH" value="" class="layui-input">
                <tip>授权的PATH。</tip>
            </div>
        </div>
        <!-- <div class="layui-form-item">
            <label class="layui-form-label">PNAME</label>
            <div class="layui-input-block">
                <input type="text" name="pName" disabled autocomplete="off"
                    placeholder="授权PNAME" value="" class="layui-input">
                <tip>授权的PNAME。</tip>
            </div>
        </div> -->
        <div class="layui-form-item">
            <label class="layui-form-label">排序值</label>
            <div class="layui-input-block">
                <input type="number" name="sort" autocomplete="off" placeholder="请输入排序值" value="0" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">授权URL</label>
            <div class="layui-input-block">
                <input type="text" name="authUrl" autocomplete="off" placeholder="请输入授权URL" value=""
                    class="layui-input">
                <tip>静态Url:/dicms/static/desktop/ui1/page/<br/><br/>APIUrl:/dicms/api/desktop/</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">授权类型</label>
            <div class="layui-input-block" id="authTypes">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">授权图标</label>
                <div class="layui-input-inline">
                    <input type="text" name="authIcon" autocomplete="off" placeholder="请输入授权图标" value=""
                        class="layui-inline layui-input">
                </div>
                <!-- <div class="layui-input-inline">
                    <i class="fa fa-adjust"></i><span>fa-adjust</span>
                </div> -->
                <div class="layui-input-inline">
                    <input type="button" id="iconBtn" value="打开图标库" class="layui-btn layui-btn-normal">    
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注信息</label>
            <div class="layui-input-block">
                <textarea name="remarks" autocomplete="off" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button id="saveBtn" class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
                <button type="reset" class="layui-btn layui-btn-danger">重置</button>
                <input type="button" class="layui-btn layui-btn-primary goback" value="返回"/>
            </div>
        </div>
    </form>
    <script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../../js/lay-config.js?v=2.0.0" charset="utf-8"></script>
    <script>
        var tableConfig = {
            editFormIdx:0,
            // 请求数据地址
            pageUrl: '../../page/icon/iconlist.html',
            
        };
        function initView(obj) {
            console.log(obj);
            var findOneUrl = "/dicms/api/desktop/sys_auth/findOne";
            var saveOneUrl = "/dicms/api/desktop/sys_auth/save";
            var findAuthTypeUrl = "/dicms/api/desktop/sys_auth_type/findAllByType";
            layui.use(['form', 'common'], function () {
                var form = layui.form,
                    layer = layui.layer,
                    common = layui.common,
                    $ = layui.$;
                if (parent.tableConfig.editMode == 0) {
                    $.ajax({
                        url: findAuthTypeUrl,
                        data: {type:'authType'},
                        type: 'get',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if (res.code !== 0) {
                                layer.msg(res.msg, function () {
                                });
                            } else {
                                if(!res.data.pName){
                                    res.data.pName = 'ROOT';
                                }
                                let authTypes = res.data;
                                let authTypesHtml = "";
                                authTypes.forEach(function(val,index){
                                    if(index==0){
                                        authTypesHtml+=`<input type="radio" name="authType" value="${val.id}" title="${val.name}" checked>`;
                                    }else{
                                        authTypesHtml+=`<input type="radio" name="authType" value="${val.id}" title="${val.name}">`;
                                    }
                                });
                                $("#authTypes").html(authTypesHtml);
                                form.val('edit-form', {pid:0,pName:"default",path:"/"});
                                // form.val('edit-form',{pName:obj.data.authName});
                            }
                        },
                        error: function (res) {
                            console.log(res);
                        }
                    });
                } else if (parent.tableConfig.editMode == 1) {
                    console.log(obj.data);
                    let id = obj.data.id;
                    $.ajax({
                        url: findOneUrl,
                        data: { id: id },
                        type: 'get',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if (res.code !== 0) {
                                layer.msg(res.msg, function () {
                                });
                            } else {
                                if(!res.data.pName){
                                    res.data.pName = 'ROOT';
                                }
                                let authTypes = res.data.authTypes;
                                let authTypesHtml = "";
                                authTypes.forEach(function(val){
                                    if(res.data.authType!=""&&res.data.authType==val.id){
                                        authTypesHtml+=`<input type="radio" name="authType" value="${val.id}" title="${val.name}" checked>`;
                                    }else{
                                        authTypesHtml+=`<input type="radio" name="authType" value="${val.id}" title="${val.name}">`;
                                    }
                                });
                                $("#authTypes").html(authTypesHtml);
                                console.log(res.data);
                                form.val('edit-form', res.data);
                                // form.val('edit-form',{pName:obj.data.authName});
                            }
                        },
                        error: function (res) {
                            console.log(res);
                        }
                    });

                } else if (parent.tableConfig.editMode == 2) {
                    console.log(obj.data);
                    let id = obj.data.id;
                    $.ajax({
                        url: findAuthTypeUrl,
                        data: {type:'authType'},
                        type: 'get',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if (res.code !== 0) {
                                layer.msg(res.msg, function () {
                                });
                            } else {
                                if(!res.data.pName){
                                    res.data.pName = 'ROOT';
                                }
                                let authTypes = res.data;
                                let authTypesHtml = "";
                                authTypes.forEach(function(val,index){
                                    if(index==0){
                                        authTypesHtml+=`<input type="radio" name="authType" value="${val.id}" title="${val.name}" checked>`;
                                    }else{
                                        authTypesHtml+=`<input type="radio" name="authType" value="${val.id}" title="${val.name}">`;
                                    }
                                });
                                $("#authTypes").html(authTypesHtml);
                                form.val('edit-form', {pid: id,path:obj.data.path});
                                // form.val('edit-form',{pName:obj.data.authName});
                            }
                        },
                        error: function (res) {
                            console.log(res);
                        }
                    });
                }

                //监听提交
                form.on('submit(saveBtn)', function (data) {   
                    data = data.field;
                    if (data.authName.length > 15) {
                        layer.msg('昵称太长!');
                        return false;
                    }
                    $("#saveBtn").attr('disabled','disabled');
                    $("#saveBtn").addClass("layui-btn-disabled");
                    $.ajax({
                        url: saveOneUrl,
                        data: data,
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if (res.code !== 0) {
                                layer.msg(res.msg, function () {
                                });
                            } else {
                                if (parent.tableConfig.editMode == 0) {
                                    common.addNodeByRoot(res.data,parent.tableConfig.tableIdx);
                                    common.closeIframe();
                                } else if (parent.tableConfig.editMode == 1) {
                                    //行内修改
                                    common.updatePNodeChildrenByTableIdx(obj,parent.tableConfig.tableIdx,res.data);
                                    common.closeIframe();
                                } else if (parent.tableConfig.editMode == 2) {
                                    //行内插入
                                    common.addNodeByPNode(obj,res.data,parent.tableConfig.tableIdx);
                                    common.closeIframe();
                                }
                            }
                        },
                        error: function (res) {
                            console.log(res);
                            $("#saveBtn").removeAttr('disabled');
                            $("#saveBtn").removeClass("layui-btn-disabled");
                        }
                    });
                    // return false;
                });

                $(".goback").click(function (e) {
                    common.closeIframe();
                });
                $("#iconBtn").click(function (e) {
                    tableConfig.editFormIdx = layer.open({
                        title: '图标库',
                        closeBtn: false,
                        type: 2,
                        shade: 0.2,
                        resize:false,
                        anim:-1,
                        // maxmin: true,
                        shadeClose: true,
                        area: ['80%', '80%'],
                        content: tableConfig.pageUrl,
                        success: function (layero, index) {
                            // common.success(layero, index, obj);
                        }
                    });
                    console.log(tableConfig.editFormIdx);
                    $(window).on("resize", function () {
                        console.log(tableConfig.editFormIdx);
                        if(tableConfig.editFormIdx){
                            layer.full(tableConfig.editFormIdx);
                        }
                    });
                });

            });
        }
    </script>
</body>

</html>